package com.sdry.service.impl.hpf;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.hpf.PositionMapper;
import com.sdry.mapper.hpf.TechnicsMapper;
import com.sdry.model.hpf.Position;
import com.sdry.model.hpf.Technics;
import com.sdry.service.hpf.TechnicsService;

/**
 * @ClassName: TechnicsServiceImpl 
 * @Description:工艺信息Service实现
 * @Author hpf
 * @Date 2018年9月21日 上午11:20:16 
 * @version 1.0
 */
@Service
public class TechnicsServiceImpl implements TechnicsService {

	@Resource
	private TechnicsMapper technicsMapper;
	@Resource
	private PositionMapper positionMapper;
	
	/**
	 * 查询总数
	 * @param technics
	 * @return
	 */
	public Integer countTechnics(Technics technics) {
		// TODO Auto-generated method stub
		return technicsMapper.countTechnics(technics);
	}

	/**
	 * 分页查询
	 * @param map
	 * @return
	 */
	public List<Technics> listPageTechnics(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return technicsMapper.listPageTechnics(map);
	}

	/**
	 * 新增工艺信息
	 * @param technics
	 * @return
	 */
	public Integer insertTechnics(Technics technics) {
		int isSuccess = -2;
		/*List<Technics> list = technicsMapper.getTechnicsByTname(technics.getTname());
		if(list.size() == 0){
			int i = technicsMapper.insertTechnics(technics);
			if(i > 0){
				isSuccess = 2;
			}
		}*/
		int i = technicsMapper.insertTechnics(technics);
		if(i > 0){
			isSuccess = 2;
		}
		return isSuccess;
	}

	/**
	 * 修改工艺信息
	 * @param technics
	 * @return
	 */
	public Integer updateTechnics(Technics technics) {
		int isSuccess = -3;
		/*Technics tec = technicsMapper.getTechnicsByTid(technics.getTid());
		if(tec.getTname().equals(technics.getTname())){
			int i = technicsMapper.updateTechnics(technics);
			if(i > 0){
				isSuccess = 3;
			}
		}else{
			List<Technics> list = technicsMapper.getTechnicsByTname(technics.getTname());
			if(list.size() == 0){
				int i = technicsMapper.updateTechnics(technics);
				if(i > 0){
					isSuccess = 3;
				}
			}
		}*/
		int i = technicsMapper.updateTechnics(technics);
		if(i > 0){
			isSuccess = 3;
		}
		return isSuccess;
	}

	/**
	 * 校验是否删除工艺信息
	 * @param tid
	 * @return
	 */
	public Integer deleteTechnicsOfVerify(String str) {
		int isSuccess = -1;
		String[] split = str.split(",");
		for(int i = 0; i < split.length; i ++){
			Long id = Long.parseLong(split[i]);
			List<Position> positionList = positionMapper.findPositionByCid(id);
			if(positionList.size() > 0){
				isSuccess = 1;
				break;
			}
		}
		return isSuccess;
	}
	
	/**
	 * 删除工艺信息
	 * @param tid
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public Integer deleteTechnics(String str) {
		int isSuccess = 0;
		try {
			String[] idArr = str.split(",");
			for (int i = 0; i < idArr.length; i++) {
				isSuccess = technicsMapper.deleteTechnics(Long.parseLong(idArr[i]));
			}
		} catch (Exception e) {
			e.printStackTrace();     
	        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	        isSuccess = 0;
		}
		return isSuccess;
	}

	/**
	 * 查询所有
	 * @param technics
	 * @return
	 */
	public List<Technics> listAllTechnics() {
		// TODO Auto-generated method stub
		return technicsMapper.listAllTechnics();
	}

	/**  
	 * 根据ID获取工艺信息     
	 * @param tid
	 * @return    
	 */
	@Override
	public Technics getTechnicsById(Long tid) {
		return technicsMapper.getTechnicsByTid(tid);
	}

	/**   
	 * 根据名称查询工艺信息    
	 * @param tname
	 * @return    
	 */
	@Override
	public List<Technics> getTechnicsByName(String tname) {
		return technicsMapper.getTechnicsByTname(tname);
	}

}
